package com.erpanderp.wam.usm.view.leave;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.hibernate.validator.xml.ValidatedByType;

import com.erpanderp.wam.adm.model.entity.Role;
import com.erpanderp.wam.usm.model.entity.leave.EmployeeAttendance;
import com.erpanderp.wam.usm.model.entity.leave.EmployeeLeave;
import com.erpanderp.wam.usm.model.entity.timesheet.TimesheetDetail;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
import com.erpanderp.wam.usm.model.entity.user.EmployeeRole;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.leave.EmployeeLeaveDAO;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDetailDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeAndReportingDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeRoleDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;
import com.erpanderp.wam.util.WAMUtil;


@ManagedBean
@ViewScoped
public class EmployeeLeaveStatusBean {
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	private Map<String, Integer> monthsMap = CommonLookup.getMonths();
	private Map<String, Integer> yearsMap = WAMUtil.getLastSixYears();
	private Map<String, String> modulesMap = new HashMap<String, String>();
	private Map<String, String> leaveStatusMap = CommonLookup.getLeaveStatus();
	private Integer selectedMonth;
	private Integer selectedYear;
	private String selectedModule;
	private boolean RENDERTABLE;
	private boolean RENDER_NO_RESULTS;
	private List<EmployeeLeave> employeeLeaves = new ArrayList<EmployeeLeave>();
	private Integer NO_OF_DAYS;
	private String selectedStatus;
	TimeZone defaultTimeZone = TimeZone.getDefault();
	private boolean FORM_VALID;
	private WAMUser wamUser = new WAMUser();
	private List<EmployeeAndReporting> employeeAndReportingList = new ArrayList<EmployeeAndReporting>();
    private Calendar calendar1;
    private  Calendar calendar2;
	public EmployeeLeaveStatusBean(){
		wamUser = ((WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER));
		Calendar calendar = Calendar.getInstance();
		selectedMonth = calendar.get(Calendar.MONTH);
		System.out.println("SELECTED MONTH ="+selectedMonth);
		selectedYear = calendar.get(Calendar.YEAR);
	}
	
	
	
	public void searchActionListener() {
		System.out.println("EMPLOYEE LEAVE STATUS LISTNER");
		employeeLeaves.clear();
		validate();
		if(FORM_VALID){
		Role role = new EmployeeRoleDAO().getRoleByEmployeeId(wamUser.getEmployee().getEmployeeId());	
		if(wamUser != null && (role!=null && role.getRole().equals(CommonLookup.STANDARD_ROLE_HR))){
		EmployeeLeaveDAO employeeLeaveDAO = new EmployeeLeaveDAO();
		calendar1 = new GregorianCalendar(selectedYear,selectedMonth, 1);
		NO_OF_DAYS = calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
		System.out.println(NO_OF_DAYS);
        calendar2 = new GregorianCalendar(selectedYear,selectedMonth, NO_OF_DAYS);
        System.out.println("FROM DATE ="+calendar1.getTime());
        System.out.println("TO DATE ="+calendar2.getTime());
        System.out.println("SELECTED STATUS ="+selectedStatus);
		employeeLeaves = employeeLeaveDAO.getEmplolyeeLeavesByStatus(calendar1.getTime(),
							                     calendar2.getTime(), selectedStatus);
		System.out.println("EMPLOYEE LEAVES SIZE ="+employeeLeaves);
        if(employeeLeaves != null && employeeLeaves.size() > 0){
        	RENDERTABLE = true;
            RENDER_NO_RESULTS = false;
        }else{
        	RENDER_NO_RESULTS = true;
        	RENDERTABLE = false;
        }
		}else{
			calendar1 = new GregorianCalendar(selectedYear,selectedMonth, 1);
			NO_OF_DAYS = calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
			System.out.println(NO_OF_DAYS);
	        calendar2 = new GregorianCalendar(selectedYear,selectedMonth, NO_OF_DAYS);
	        System.out.println("FROM DATE ="+calendar1.getTime());
	        System.out.println("TO DATE ="+calendar2.getTime());
			employeeAndReportingList = new EmployeeAndReportingDAO().getAllEmployeesUnderReporting(wamUser.getEmployeeCode());
			EmployeeLeaveDAO employeeLeaveDAO = new EmployeeLeaveDAO();
			System.out.println("REPORTING LIST "+employeeAndReportingList.size());
			int i = 0;
			for(EmployeeAndReporting employeeAndReporting  : employeeAndReportingList){
				System.out.println("EMPLOYEE CODE ="+employeeAndReporting.getEmployee().getEmployeeCode());
				employeeLeaves = employeeLeaveDAO.getEmplolyeeLeavesByStatus(calendar1.getTime(),
	                     calendar2.getTime(), selectedStatus,wamUser.getEmployee().getEmployeeId());
				i++;
			}
			System.out.println("I ="+i);

			 if(employeeLeaves != null && employeeLeaves.size() > 0){
		        	RENDERTABLE = true;
		            RENDER_NO_RESULTS = false;
		        }else{
		        	RENDER_NO_RESULTS = true;
		        	RENDERTABLE = false;
		        }
		}
		}else{
			RENDER_NO_RESULTS = false;
        	RENDERTABLE = false;
		}
			
	}
	public void validate() {
		FORM_VALID = true;

		if (ValidationUtil.isEmpty(selectedStatus, "Leave status","", FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}

		
	}
	
	
	

	public Map<String, Integer> getMonthsMap() {
		return monthsMap;
	}

	public void setMonthsMap(Map<String, Integer> monthsMap) {
		this.monthsMap = monthsMap;
	}

	public Map<String, Integer> getYearsMap() {
		return yearsMap;
	}

	public void setYearsMap(Map<String, Integer> yearsMap) {
		this.yearsMap = yearsMap;
	}

	public Map<String, String> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, String> modulesMap) {
		this.modulesMap = modulesMap;
	}

	public Integer getSelectedMonth() {
		return selectedMonth;
	}

	public void setSelectedMonth(Integer selectedMonth) {
		this.selectedMonth = selectedMonth;
	}

	public Integer getSelectedYear() {
		return selectedYear;
	}

	public void setSelectedYear(Integer selectedYear) {
		this.selectedYear = selectedYear;
	}

	public String getSelectedModule() {
		return selectedModule;
	}

	public void setSelectedModule(String selectedModule) {
		this.selectedModule = selectedModule;
	}

	public Map<String, String> getLeaveStatusMap() {
		return leaveStatusMap;
	}

	public void setLeaveStatusMap(Map<String, String> leaveStatusMap) {
		this.leaveStatusMap = leaveStatusMap;
	}

	public boolean isRENDERTABLE() {
		return RENDERTABLE;
	}

	public void setRENDERTABLE(boolean rENDERTABLE) {
		RENDERTABLE = rENDERTABLE;
	}



	public List<EmployeeLeave> getEmployeeLeaves() {
		return employeeLeaves;
	}



	public void setEmployeeLeaves(List<EmployeeLeave> employeeLeaves) {
		this.employeeLeaves = employeeLeaves;
	}



	public Integer getNO_OF_DAYS() {
		return NO_OF_DAYS;
	}



	public void setNO_OF_DAYS(Integer nO_OF_DAYS) {
		NO_OF_DAYS = nO_OF_DAYS;
	}



	public String getSelectedStatus() {
		return selectedStatus;
	}



	public void setSelectedStatus(String selectedStatus) {
		this.selectedStatus = selectedStatus;
	}



	public TimeZone getDefaultTimeZone() {
		return defaultTimeZone;
	}



	public void setDefaultTimeZone(TimeZone defaultTimeZone) {
		this.defaultTimeZone = defaultTimeZone;
	}



	public boolean isRENDER_NO_RESULTS() {
		return RENDER_NO_RESULTS;
	}



	public void setRENDER_NO_RESULTS(boolean rENDER_NO_RESULTS) {
		RENDER_NO_RESULTS = rENDER_NO_RESULTS;
	}



	public boolean isFORM_VALID() {
		return FORM_VALID;
	}



	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}



	public WAMUser getWamUser() {
		return wamUser;
	}



	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}



	public List<EmployeeAndReporting> getEmployeeAndReportingList() {
		return employeeAndReportingList;
	}



	public void setEmployeeAndReportingList(List<EmployeeAndReporting> employeeAndReportingList) {
		this.employeeAndReportingList = employeeAndReportingList;
	}

}
